package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinDetailResult;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/19.
 */

public class AttrAdapter extends BaseAdapter {
    private Context mContext;
    private List<RewardCoinDetailResult.Attr> attr;
    private List<Map<String, String>> list = new ArrayList<Map<String, String >>();

    public AttrAdapter(Context context, List<RewardCoinDetailResult.Attr> attr) {
        this.mContext = context;
        this.attr = attr;
    }

    @Override
    public int getCount() {
        return attr.size();
    }

    public List<Map<String, String>> getList() {
        return list;
    }

    public void setList(List<Map<String, String>> list) {
        this.list = list;
    }

    @Override
    public Object getItem(int position) {
        return attr.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try{
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_attr, parent, false);
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                holder.gvList = (NoScrollGridView) convertView.findViewById(R.id.gvList);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if(null!=attr.get(position)&&!"".equals(attr.get(position))){
                holder.tvTitle.setText(attr.get(position).getName()+"选择");
                final AttrValueAdapter adapter = new AttrValueAdapter(mContext, attr.get(position).getValue());
                holder.gvList.setAdapter(adapter);
                //单选框
                holder.gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(attr.get(position).getName(), attr.get(position).getValue().get(pos));
                        list.add(map);
                        adapter.setSelect(pos);
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder{
        private TextView tvTitle;
        private NoScrollGridView gvList;
    }
}
